<?php


namespace App\Admin\Override;

use App\Cache\Maintenance;
use App\Utils\Helper;
use Dcat\Admin\Admin;
use Dcat\Admin\Tree as Base;


class Tree extends Base
{
    public $params;
    public function __construct($repository = null, \Closure $callback = null)
    {
        $this->showQuickCreateButton(false)
            ->showQuickEditButton(false)
            ->showCreateButton()
            ->showEditButton()
            ->showDeleteButton()
            ->showSaveButton();
        // must be at the end
        parent::__construct($repository, $callback);
    }

    public function showCreateButton(bool $value = true)
    {
        $this->disableCreateButton(
            !$value || Admin::user()->cannot(Helper::resolvePermissionRouteName('store'))
        );

        return $this;
    }

    public function showQuickCreateButton(bool $value = true)
    {
        $this->disableQuickCreateButton(
            !$value || Admin::user()->cannot(Helper::resolvePermissionRouteName('store'))
        );

        return $this;
    }

    public function showQuickEditButton(bool $value = true)
    {
        $this->disableQuickEditButton(
            !$value || Admin::user()->cannot(Helper::resolvePermissionRouteName('update'))
        );

        return $this;
    }

    public function showDeleteButton(bool $value = true)
    {
        $this->disableDeleteButton(
            !$value || Admin::user()->cannot(Helper::resolvePermissionRouteName('destroy'))
        );

        return $this;
    }

    public function showSaveButton(bool $value = true)
    {
        $this->disableSaveButton(
            !$value || Admin::user()->cannot(Helper::resolvePermissionRouteName('update'))
        );

        return $this;
    }

    public function showEditButton(bool $value = true)
    {
        $this->disableEditButton(
            !$value || Admin::user()->cannot(Helper::resolvePermissionRouteName('update'))
        );

        return $this;
    }

    /**
     * @return string
     */
    protected function renderCreateButton()
    {
        if (! $this->useQuickCreate && ! $this->useCreate) {
            return '';
        }

        $url = $this->url.'/create';
        if($this->params){
            $count = 0;
            foreach ($this->params as $p_k => $p_v){
                if($count == 0){
                    $url = $url.'?'.$p_k.'='.$p_v;
                }else{
                    $url = $url.'&'.$p_k.'='.$p_v;
                }
                $count++;
            }
        }
        $new = trans('admin.new');

        $quickBtn = $btn = '';
        if ($this->useCreate) {
            $btn = "<a href='{$url}' class='btn btn-sm btn-primary'><i class='feather icon-plus'></i><span class='d-none d-sm-inline'>&nbsp;{$new}</span></a>";
        }

        if ($this->useQuickCreate) {
            $text = $this->useCreate ? '<i class=\' fa fa-clone\'></i>' : "<i class='feather icon-plus'></i><span class='d-none d-sm-inline'>&nbsp; $new</span>";
            $quickBtn = "<button data-url='$url' class='btn btn-sm btn-primary tree-quick-create'>$text</button>";
        }

        return "&nbsp;<div class='btn-group pull-right' style='margin-right:3px'>{$btn}{$quickBtn}</div>";
    }

    /**
     * @param string $field
     */
    public function disableButtonOnMaintenance(string $field)
    {
        if (Maintenance::isNotMaintenance($field)) {
            $this->showQuickCreateButton(false)
                ->showCreateButton(false)
                ->showQuickEditButton(false)
                ->showEditButton(false)
                ->showDeleteButton(false)
                ->showSaveButton(false);
        }
    }

}
